package com.app.sanyou.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.app.sanyou.entity.LoginInfo;

//本sqlite数据库只是为了暂存用户的账号密码
public class LoginDBHelper extends SQLiteOpenHelper {
    //应该配置在xml里更好
    private static final String DB_NAME = "login.db";
    private static final String TABLE_NAME = "user_info";
    //版本名
    private static final int DB_VERSION = 1;
    //唯一实例
    private static LoginDBHelper mHelper=null;
    //读写锁
    private SQLiteDatabase mRDB=null;
    private SQLiteDatabase mWDB=null;

    private LoginDBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    //单例模式
    public static LoginDBHelper getInstance(Context context)
    {
        if(mHelper==null)
        {
            mHelper=new LoginDBHelper(context);
        }
        return mHelper;
    }

    //打开数据库的读连接
    public SQLiteDatabase openReadLink()
    {
        if(mRDB==null||!mRDB.isOpen())
            mRDB=mHelper.getReadableDatabase();
        return mRDB;
    }
    //打开数据库的写连接
    public SQLiteDatabase openWriteLink()
    {
        if(mWDB==null||!mWDB.isOpen())
            mWDB=mHelper.getWritableDatabase();
        return mWDB;
    }

    //关闭数据库
    public  void closeLink()
    {
        if (mRDB!=null&&mRDB.isOpen()) {
            mRDB.close();
            mRDB=null;
        }
        if (mWDB!=null&&mWDB.isOpen()) {
            mWDB.close();
            mWDB=null;
        }
    }

    //创建数据库的表
    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql="CREATE TABLE IF NOT EXISTS " +TABLE_NAME +
                "(" +
                "_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                " username VARCHAR NOT NULL," +
                " password VARCHAR NOT NULL," +
                " remember INTEGER NOT NULL" +
                ");";
        db.execSQL(sql);
    }

    //更新数据库版本时的操作
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //增加字段
//        String sql="ALTER TABLE "+TABLE_NAME+" ADD COLUMN phone VARCHAR;";
//        db.execSQL(sql);
//        sql="ALTER TABLE "+TABLE_NAME+" ADD COLUMN password VARCHAR;";
//        db.execSQL(sql);
    }

    public long insert(LoginInfo info)
    {
        //这是一个map类型
        ContentValues values=new ContentValues();
        values.put("username",info.username);
        values.put("password",info.password);
        values.put("remember",info.remember);
        //返回行号，-1代表插入失败
        return mWDB.insert(TABLE_NAME,null,values);

        //当插入多条记录时，需要开启事务
        /*try {
            //
            mWDB.beginTransaction();
            mWDB.insert(TABLE_NAME,null,values);
            mWDB.insert(TABLE_NAME,null,values);

            mWDB.setTransactionSuccessful();
        }catch (Exception e)
        {
            e.printStackTrace();
        }finally {
            mWDB.endTransaction();
        }
        return 1;*/
    }
    //读取最后一个插入的
    public LoginInfo queryTop()
    {
        LoginInfo info=null;

        String sql="select * from "+
                TABLE_NAME+
                " where remember=1 order by _id desc limit 1";

        Cursor cursor = mWDB.rawQuery(sql, null);

        if (cursor.moveToNext()) {
            info=new LoginInfo();
            info.id=cursor.getInt(0);
            info.username=cursor.getString(1);
            info.password=cursor.getString(2);
            //只能用这种方法来判断布尔类型
            info.remember=cursor.getInt(3)!=0;
        }
        return info;
    }
    public void save(LoginInfo info){
        //先删除再增加
        //由于需要多次和数据库打交道，需要创建事务
        try {
            //
            mWDB.beginTransaction();
            delete(info);
            insert(info);
            mWDB.setTransactionSuccessful();
        }catch (Exception e)
        {
            e.printStackTrace();
        }finally {
            mWDB.endTransaction();
        }
    }
    public long delete(LoginInfo info)
    {
        return mWDB.delete(TABLE_NAME,"username=?",new String[]{info.username});
    }

}
